//
//  NetServer.h
//  CommandCenter08
//
//  Created by Matt Roman on 7/16/08.
//  Copyright 2008 University of Oklahoma. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <netinet/in.h>

#define	MAXNUMCONNECTIONS	5

@interface NetServer : NSObject {
	CFSocketRef			socketRef;
	CFRunLoopSourceRef	nSocketRunLoopSource;
	struct sockaddr_in	socketAddress;
	struct sockaddr_in	blankAddress;
	socklen_t			socketAddressLen;
	id					nDelegate;
	NSMutableData*		nDataOut;
	NSMutableData*		nDataIn;
	//struct sockaddr_in	connectionsList[MAXNUMCONNECTIONS];
	struct sockaddr_in	aConnection;
	int					numConnections;
}

+ (NetServer*)UDPServerSocket;

// Delegate
- (void)setDelegate:(id)inDelegate;

- (BOOL)openSocket;
- (void)close;
- (int)numConnections;
// Runloop Scheduling
- (BOOL)scheduleOnCurrentRunLoop;
- (BOOL)scheduleOnRunLoop:(NSRunLoop*)inRunLoop;
- (void)unscheduleFromRunLoop;

// Connecting
- (BOOL)bindServerToPort:(UInt16)inPort;

// Writing data
- (void)sendData:(NSData*)inData;
- (void)sendString:(NSString*)inString encoding:(NSStringEncoding)inEncoding;

// Reading data
- (NSData*)readData;
- (NSData*)readData:(unsigned)inAmount;
- (NSString*)readString:(NSStringEncoding)inEncoding;

- (void) flushBuffers;
@end

@interface NSObject (NetServerDelegate)
- (void)netServerConnection:(NetServer*)inNetServer;
- (void)netDataSent:(NetServer*)inNetServer;
- (void)netServerDisconnected:(NetServer*)inNetServer;
- (void)netDataAvailable:(NetServer*)inNetServer amount:(unsigned)inAmount;
@end